package com.kstech.test.padxmledit;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by lenovo on 2016/9/7.
 * 编辑数据页面的list fragment
 */
public class EditListFragMent extends Fragment{
    private ListView editDataList;
    private Button btnBack;
    private EditAdapter adapter;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0){
                adapter.notifyDataSetChanged();
            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.data_edit_list,null);
        editDataList = (ListView) view.findViewById(R.id.list_data_edit);
        btnBack = (Button) view.findViewById(R.id.btn_back);
        adapter = new EditAdapter();
        editDataList.setAdapter(adapter);
        editDataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Globals.qcItem = Globals.list_edit.get(i);
                EditFragMent editFragMent = new EditFragMent();
                FragmentTransaction transaction =getFragmentManager().beginTransaction();
                transaction.replace(R.id.ll_data_edit,editFragMent);
                transaction.commit();
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Globals.list_edit.clear();
                Globals.qcItem =null;
                getActivity().finish();
            }
        });
        return view;
    }
    class EditAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return Globals.list_edit.size();
        }

        @Override
        public Object getItem(int i) {
            return Globals.list_edit.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(getActivity(), R.layout.list_item_dataset, null);
            }
            TextView tv = (TextView) view.findViewById(R.id.pad_tv_data);
            if (Globals.list_edit.size() == 0) {
                tv.setText("没有数据，请稍后。。。。");
            } else {
                tv.setText(Globals.list_edit.get(i).toString());
            }
            return view;
        }
    }
}
